//
//  LVBaseTableViewModel.m
//  BaseCode
//
//  Created by 曹连华 on 2019/6/20.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "LVBaseTableViewModel.h"

@interface LVBaseTableViewModel ()

@property (nonatomic, strong) LVTableViewDataSource *dataSource;

@end

@implementation LVBaseTableViewModel

- (LVTableViewDataSource *)dataSource {
    if (!_dataSource) {
        _dataSource = [[LVTableViewDataSource alloc]init];
    }
    return _dataSource;
}

- (void)initOperateBlock {
    self.dataSource.removeRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable indexPath) {
        operateResultBlock(YES);
    };
    self.dataSource.insertRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable indexPath) {
        operateResultBlock(YES);
    };
    self.dataSource.moveRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable sourceIndexPath, NSIndexPath * _Nullable destinationIndexPath) {
        operateResultBlock(YES);
    };
}

- (void)reloadTableView {
    [self reloadTableViewWithIndexPaths:nil];
}

- (void)reloadTableViewWithIndexPaths:(nullable NSArray<NSIndexPath *>*)indexPaths {
    if(self.reloadTableViewBlock) {
        self.reloadTableViewBlock(nil,indexPaths);
    }
}

- (void)reloadSectionsWithSections:(NSIndexSet *)sections {
    if(self.reloadTableViewBlock) {
        self.reloadTableViewBlock(sections,nil);
    }
}

- (LVBaseCellRowModel *)didSelectedRowModelWithIndexPath:(NSIndexPath *)indexPath {
    return [self didSelectedRowModelWithIndexPath:indexPath completionBlock:nil];
}

- (LVBaseCellRowModel *)didSelectedRowModelWithIndexPath:(NSIndexPath *)indexPath completionBlock:(nullable LVCompletionBlock)completionBlock {
    if (completionBlock) {
        completionBlock(nil);
    }
    return [self.dataSource rowModelAtIndex:indexPath];
}

@end
